﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kovan.Model.Entity.Config;
using System.Linq.Expressions;
using Kovan.Model.Parameter.Config;
using Kovan.Library.Extensions;

namespace Kovan.DAL.Config
{
    public class DAd : IDAL<tb_ad>
    {
        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<tb_ad> GetList(PAd param)
        {
            using (var db = new MysqlContext())
            {
                Expression<Func<tb_ad, bool>> wh = c => c != null;

                var query = db.tb_ad.OrderByDescending(c => c.ad_id.Value).AsQueryable();

                if (param.applicationId.HasValue)
                {
                    wh = wh.And(c => c.application_id == param.applicationId.Value);
                }

                if (param.isEnable.HasValue)
                {
                    wh = wh.And(c => c.is_enable == param.isEnable.Value);
                }

                if (!string.IsNullOrEmpty(param.adName))
                {
                    wh = wh.And(c => c.ad_name.Contains(param.adName));
                }

                if (!string.IsNullOrEmpty(param.fileName))
                {
                    wh = wh.And(c => c.file_name.Contains(param.fileName));
                }

                //总条数
                param.TotalRows = query.Where(wh).Count();

                if (param.PageSize > 0 && param.PageIndex > 0)
                {
                    query = query.Skip(param.PageSize * (param.PageIndex - 1)).Take(param.PageSize);
                }

                query = query.Where(wh);

                return param.TotalRows > 0 ? query.ToList() : null;
            }
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public tb_ad GetModel(long id)
        {
            using (var db = new MysqlContext())
            {
                return db.tb_ad.FirstOrDefault(c => c.ad_id == id);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(tb_ad model)
        {
            using (var db = new MysqlContext())
            {
                db.tb_ad.Add(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(long id)
        {
            using (var db = new MysqlContext())
            {
                var model = db.tb_ad.FirstOrDefault(a => a.ad_id == id);
                db.tb_ad.Remove(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int BatchDelete(IEnumerable<string> ids)
        {
            using (var db = new MysqlContext())
            {
                var files = new List<string>();
                foreach (var id in ids)
                {
                    var adid = long.Parse(id);
                    var model = db.tb_ad.FirstOrDefault(a => a.ad_id == adid);
                    if (model != null)
                    {
                        files.Add(model.file_name);
                        db.tb_ad.Remove(model);

                    }
                }
                ids = files;
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 批量删除(返回js文件路径集合)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int BatchDelete(IEnumerable<string> ids,out List<string> files)
        {
            using (var db = new MysqlContext())
            {
                files = new List<string>();
                foreach (var id in ids)
                {
                    var adid = long.Parse(id);
                    var model = db.tb_ad.FirstOrDefault(a => a.ad_id == adid);
                    if (model != null)
                    {
                        files.Add(model.file_name);
                        db.tb_ad.Remove(model);

                    }
                }
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(tb_ad model)
        {
            using (var db = new MysqlContext())
            {
                db.Set<tb_ad>().Attach(model);
                db.Entry(model).State = System.Data.EntityState.Modified;
                return db.SaveChanges();
            }
        }


    }
}
